package cn.powertime.evaluation.app.controller;

import cn.powertime.evaluation.facade.CpsUserCollectFacade;
import cn.powertime.evaluation.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 用户收藏习题表 前端控制器
 * </p>
 *
 * @author Yang Xin
 * @since 2019-10-9
 */
@RestController
@RequestMapping("/app/userCollect")
@Api(value = "/app/userCollect", tags = "用户收藏习题接口", produces = MediaType.ALL_VALUE)
@Validated
public class AppUserCollectController extends BaseController {

    @Autowired
    private CpsUserCollectFacade userCollectFacade;

    @ApiOperation(value = "习题收藏/取消收藏接口")
    @PostMapping(value = "/collect")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型,1:收藏，2:取消收藏", required = true, example = "1"),
            @ApiImplicitParam(name = "topicId", value = "习题id", required = true)})
    public Object collect(@NotNull(message = "类型,1:收藏，2:取消收藏") @Max(value = 1, message = "类型最大值为1") @Min(value = 0, message = "类型最小值为0") @RequestParam(defaultValue = "1") Integer type, @NotNull(message = "习题id不能为空") @RequestParam Long topicId) {
        boolean b = this.userCollectFacade.collect(type, topicId);
        String note = null;
        if (type == 1) {
            note = "收藏习题";
        } else if (type == 0) {
            note = "取消收藏习题";
        }
        if (b) {
            return success(note + "成功");
        }
        return error(note + "失败");
    }

}
